<?php
/**
 * Customer Controller
 *
 * @author Steve
 * @version
 */ 

require_once   BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once   BaseUtil::getModulePath("gift")  .  '/services/GiftService.php';
require_once   BaseUtil::getModulePath("common")  .  '/services/CommonService.php';
require_once BaseUtil::getModulePath("promotion")  .  '/services/PromotionService.php';
require_once BaseUtil::getModulePath("customer")  .  '/services/CustomerService.php';
require_once   BaseUtil::getModulePath("gift")  .  '/constants/GiftConstant.php';
require_once   BaseUtil::getModulePath("order")  .  '/constants/OrderConstant.php';

class Gift_ApplygiftlogController extends BaseController {

	public function init(){
		parent::init();
		$this->view->script= array('gift.js');
	}
	
	public function indexAction(){
		$giftService = new GiftService();
		$applyGiftLogs = $giftService->getApplyGiftLogList($_POST,$this->view);
		$this->view->applygiftLogs = $applyGiftLogs;
	}
	
	public function editAction(){
		$id = $this->_getParam('id');
		
		$giftService = new GiftService();
		$applyGiftLog = $giftService->getApplyGiftLogById($id);
		if($applyGiftLog['apply_type']==1 && $applyGiftLog['promotion_id']){
			$promotionService = new PromotionService();
			$promotion = $promotionService->getPromotionByid($applyGiftLog['promotion_id']);
			$this->view->promotion = $promotion;
		} else if($applyGiftLog['apply_type']==2 && $applyGiftLog['gift_id']){
			$gift = $giftService->getGiftById($applyGiftLog['gift_id']);
			$commonService = new CommonService();
			$proince = $commonService->getDictTitleById($applyGiftLog['delivery_address1']);
			$city = $commonService->getDictTitleById($applyGiftLog['delivery_address2']);
			$county = $commonService->getDictTitleById($applyGiftLog['delivery_address3']);
			
			$this->view->dict = $proince.' '.$city.' '.$county;
			$this->view->gift = $gift;	
		}
		$this->view->id = $id;
		$this->view->applyGiftLog = $applyGiftLog;
	}
	
	public function subeditAction(){
		$id = HttpUtil::getPostString('id');
		
		//$applyType = HttpUtil::getPostString('apply_type');
		
		$applyGiftLog['shipping_method_id'] = HttpUtil::getPostString('shipping_method');
		$applyGiftLog['tracking_num'] = HttpUtil::getPostString('tracking_num');
		$applyGiftLog['status'] = HttpUtil::getPostString('apply_gift_status');
		
		$applyGiftLog['description'] = HttpUtil::getPostString('description');
		$giftService = new GiftService();
		$giftService->updateApplyGiftLog($applyGiftLog,$id);
		if(isset($applyGiftLog['status']) && $applyGiftLog['status']==4){
			$points = HttpUtil::getPostString('points');
			$userId = HttpUtil::getPostString('user_id');
			$customerService = new CustomerService();
			$customerService->returnPoints($points,$userId);
		}
		$this->_redirector->gotoUrl('/gift/applygiftlog');
	}
	
	public function deleteAction(){
		$id = $this->_getParam('id');
		$giftService = new GiftService();
		$giftService->deleteApplyGiftLog($id);
		$this->_redirector->gotoUrl('/gift/applygiftlog');
	}
}
?>